import { getDetail, getList } from '../services/api'
export default {
  
  namespace: 'tagLandingPage',
  
  state: {
    recommend: [],
    share_info: [],
    tabs: [],
    hotPlay: [],
    hotPlace: [],
    tag: {},
    list: [],
    next: false,
    next_boundary: 0
  },
  
  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    },
  },
  
  effects: {
    *getDetail({ payload, callback }, { call, put, select }) {
      const response = yield call( getDetail, payload );
      let hotPlay = [], hotPlace = [];
      if (response){
        const { recommend, share_info, tabs, tag }  = response;
        document.getElementsByTagName("title")[0].innerText = tag.name;
        if( recommend && recommend.length > 0 ){
          recommend.forEach( item => {
            if( item.style && item.style == 'hot_play' ){
              hotPlay.push(item.data);
            } else if( item.style && item.style == 'hot_place' ){
              hotPlace.push(item.data)
            }
          })
        }
        
        yield put({
          type: 'saveState',
          payload: {
            recommend: recommend,
            share_info: share_info,
            tabs: tabs,
            tag: tag,
            hotPlay: hotPlay,
            hotPlace: hotPlace,
          },
        });
  
        callback(tabs);
      }
    },
    
    *getList({ payload, callback }, { call, put, select }) {
      const response = yield call( getList, payload );
      if (response){
        const { list, page } = response;
        yield put({
          type: 'saveState',
          payload: {
            list: list,
            next: page.next,
            next_boundary: page.next_boundary
          },
        });
      }
    },
  },
  
  reducers: {
    saveState(state, action) {
      return {
        ...state,
        ...action.payload
      };
    },
  },
  
};
